#include "login.h"
#include "ui_login.h"
#include<QToolButton>
#include<QPixmap>
#include "widget.h"

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    QVector<QToolButton*> vector;
    ui->setupUi(this);

    // ==== 窗口基础设置 ====
    this->setWindowIcon(QIcon(":/images/qq.png"));
    this->setWindowTitle("QQ 登录");
    this->setFixedSize(300, 700); // 固定窗口大小避免拉伸变形

    // 设置背景渐变
    QPalette palette(this->palette());
    QLinearGradient gradient(0, 0, 0, height());
    gradient.setColorAt(0, QColor(240, 245, 255)); // 浅蓝
    gradient.setColorAt(1, QColor(220, 230, 255)); // 淡紫蓝
    palette.setBrush(QPalette::Window, QBrush(gradient));
    this->setPalette(palette);

    // ==== 工具盒样式优化 ====
    ui->toolBox->setStyleSheet("QToolBox::tab { background-color: #E6EEFF; color: #333; height: 30px; }"
                               "QToolBox::tab:selected { background-color: #C1D3FE; font-weight: bold; }");

    // ==== 用户按钮列表 ====
    QStringList list = {"林品如", "洪世贤", "蔡徐坤", "哈基米", "魔丸", "灵珠", "川建国", "瞌睡乔"};
    QStringList iconList = {"lpr", "hsx", "cxk", "hjm", "mw", "lz", "cjg", "ksq"};
    QSize iconSize(64, 64);

    // 调整布局间距
    ui->vlayout->setSpacing(10); // 按钮之间间距
    ui->vlayout->setContentsMargins(20, 20, 20, 20); // 边缘留白

    for(int i=0; i<8; i++){
        QToolButton *btn = new QToolButton(this);
        // 加载图片
        QPixmap pixmap(QString(":/images/%1.png").arg(iconList[i]));
        btn->setIcon(QIcon(pixmap.scaled(iconSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
        btn->setIconSize(iconSize);

        // 按钮样式优化（圆角、悬停效果）
        btn->setStyleSheet("QToolButton {"
                           "background-color: white;"
                           "border: 1px solid #DDD;"
                           "border-radius: 8px;"
                           "padding: 8px;"
                           "text-align: left;"
                           "}"
                           "QToolButton:hover {"
                           "background-color: #F0F7FF;"
                           "border-color: #A8C7FF;"
                           "}"
                           "QToolButton:pressed {"
                           "background-color: #E6EEFF;"
                           "}");

        btn->setText(list[i]);
        btn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        btn->setFixedHeight(80); // 固定按钮高度
        btn->setMinimumWidth(240); // 最小宽度

        ui->vlayout->addWidget(btn);
        vector.push_back(btn);
        isShow.push_back(false);
    }

    // 底部添加弹簧占位
    ui->vlayout->addStretch();

    // 连接按钮点击事件（保持原有逻辑）
    for(int i=0; i<8; i++){
        connect(vector[i], &QToolButton::clicked, [=](){
            if(isShow[i]){
                QMessageBox::warning(this, "警告", "该聊天框已被打开");
                return;
            }
            isShow[i] = true;
            Widget *widget = new Widget(0, vector[i]->text());
            widget->setWindowIcon(vector[i]->icon());
            widget->setWindowTitle(vector[i]->text() + " - QQ聊天");
            widget->show();
            connect(widget, &Widget::closeWidget, [=](){
                isShow[i] = false;
            });
        });
    }
}


Login::~Login()
{
    delete ui;
}
